//
//  ZsbWorkOrdorSearchCell.m
//  HCNewGoldFinger
//
//  Created by 刘琛 on 2017/12/15.
//  Copyright © 2017年 zjhcsoftios. All rights reserved.
//

#import "ZsbWorkOrdorSearchCell.h"
#import "UIColor+Ex.h"

@interface ZsbWorkOrdorSearchCell()<UISearchBarDelegate>

@property (weak, nonatomic) IBOutlet UIButton *titleButton;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *titleButtonWidth;
@property (weak, nonatomic) IBOutlet UISearchBar *searchBar;
@property (nonatomic, strong) ZSBSearchDataModel *descOrAscModel;

@property (weak, nonatomic) IBOutlet UIButton *timeButton;

@end


@implementation ZsbWorkOrdorSearchCell

- (void)awakeFromNib {
    [super awakeFromNib];
    
    self.searchBar.showsCancelButton = YES;
    self.searchBar.delegate = self;
    
    
    _timeButton.layer.borderColor = [UIColor colorWithHexString:@"169BD5"].CGColor;
    _timeButton.layer.borderWidth = 1;
    _timeButton.layer.cornerRadius = 2;

}
- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar {
    [searchBar resignFirstResponder];
}

- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar {
    
    [searchBar resignFirstResponder];
}

- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText {
    _model.searchText = searchBar.text;
    
}

- (void)searchBarTextDidEndEditing:(UISearchBar *)searchBar {
    if ([_delegate respondsToSelector:@selector(searchCellSearchBarFinished:AtIndexPath:) ]) {
        [_delegate searchCellSearchBarFinished:_model AtIndexPath:_indexPath];
    }
}




- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}


- (void)setModel:(ZSBSearchModel *)model {
    _model = model;
    [self.titleButton setTitle:model.name forState:UIControlStateNormal];
    self.titleButtonWidth.constant = [self getLabelSizeWithString:model.name];
    self.searchBar.text = model.searchText;
    
    if ([_model.orderType isEqualToString:@"number"] || [_model.orderType isEqualToString:@"varchar"]) {
        self.searchBar.hidden = NO;
        self.timeButton.hidden = YES;
        
        if ([_model.orderType isEqualToString:@"number"]) {
            self.searchBar.keyboardType = UIKeyboardTypePhonePad;
        }else if ([_model.orderType isEqualToString:@"varchar"]) {
            self.searchBar.keyboardType = UIKeyboardTypeDefault;
        }
        
    }else if([_model.orderType isEqualToString:@"date"]) {
        self.searchBar.hidden = YES;
        self.timeButton.hidden = NO;
        
    }
    
}

- (CGFloat)getLabelSizeWithString:(NSString *) text {
    
    CGSize size = [text sizeWithAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:14],NSFontAttributeName,nil]];
    
    return size.width + 10;
}

- (IBAction)titleButtonAction:(UIButton *)sender {
    
    if ([_delegate respondsToSelector:@selector(titleButtonActionWithIndexPath:)]) {
        [_delegate titleButtonActionWithIndexPath:_indexPath];
    }
    
}

- (IBAction)timeButtonAction:(UIButton *)sender {
    
    if ([_delegate respondsToSelector:@selector(timeButtonActionWithIndexPath:)]) {
        [_delegate timeButtonActionWithIndexPath:_indexPath];
    }
    
}


@end
